#!/bin/sh -e

# Clean Build
rm -rf "./Build"
rm -rf "./SDK"

REVEAL_XCFRAMEWORK_IN_FINDER=true
PROJECT_DIR=$(pwd)
PROJECT_NAME="LKSportSDKPosePosture"
FREAMEWORK_NAME="LKSportSDKPosePosture"
FREAMEWORK_OUTPUT_DIR="${PROJECT_DIR}/SDK"
ARCHIVE_PATH_IOS_DEVICE="./Build/ios_device.xcarchive"
ARCHIVE_PATH_IOS_SIMULATOR="./Build/ios_simulator.xcarchive"
# ARCHIVE_PATH_MACOS="./build/macos.xcarchive"

function archiveOnePlatform {
    echo "▸ Starts archiving the scheme: ${1} for destination: ${2};\n▸ Archive path: ${3}"

    xcodebuild archive \
        -workspace "Example/${PROJECT_NAME}.xcworkspace" \
        -scheme "${1}" \
        -configuration Release \
        -sdk "${2}" \
        -archivePath "${3}" \
        SKIP_INSTALL=NO \
        BUILD_LIBRARY_FOR_DISTRIBUTION=YES

    # sudo gem install -n /usr/local/bin xcpretty
    # xcpretty makes xcode compile information much more readable.
}

function archiveAllPlatforms {

    # https://www.mokacoding.com/blog/xcodebuild-destination-options/

    # Platform                Destination
    # iOS                    generic/platform=iOS
    # iOS Simulator            generic/platform=iOS Simulator
    # iPadOS                generic/platform=iPadOS
    # iPadOS Simulator        generic/platform=iPadOS Simulator
    # macOS                    generic/platform=macOS
    # tvOS                    generic/platform=tvOS
    # watchOS                generic/platform=watchOS
    # watchOS Simulator        generic/platform=watchOS Simulator
    # carPlayOS                generic/platform=carPlayOS
    # carPlayOS Simulator    generic/platform=carPlayOS Simulator

    SCHEME=${1}

    archiveOnePlatform $SCHEME "iphonesimulator" ${ARCHIVE_PATH_IOS_SIMULATOR}
    archiveOnePlatform $SCHEME "iphoneos" ${ARCHIVE_PATH_IOS_DEVICE}
      # archiveOnePlatform $SCHEME "generic/platform=macOS" ${ARCHIVE_PATH_MACOS}
}

function makeXCFramework {

    FRAMEWORK_RELATIVE_PATH="Products/Library/Frameworks"
    OUTPUT_DIR="${FREAMEWORK_OUTPUT_DIR}"

    mkdir -p "${OUTPUT_DIR}"

    xcodebuild -create-xcframework \
        -framework "${ARCHIVE_PATH_IOS_DEVICE}/${FRAMEWORK_RELATIVE_PATH}/${FREAMEWORK_NAME}.framework" \
        -framework "${ARCHIVE_PATH_IOS_SIMULATOR}/${FRAMEWORK_RELATIVE_PATH}/${FREAMEWORK_NAME}.framework" \
        -output "${OUTPUT_DIR}/${FREAMEWORK_NAME}.xcframework"
}

echo "#####################"
echo "▸ Cleaning XCFramework output dir: ${FREAMEWORK_OUTPUT_DIR}"
rm -rf $FREAMEWORK_OUTPUT_DIR

#### Make XCFramework

echo "▸ Archive framework: ${FREAMEWORK_NAME}"
archiveAllPlatforms $FREAMEWORK_NAME

echo "▸ Make framework: ${FREAMEWORK_NAME}.xcframework"
makeXCFramework

# Clean Build
rm -rf "./Build"

if [ ${REVEAL_XCFRAMEWORK_IN_FINDER} = true ]; then
    open "${FREAMEWORK_OUTPUT_DIR}/"
fi
